/*ViewRejectedPhoto.java
 * 
 * Activity to view individual rejected photo close up
 * 
 *  Shared Preferences code based on example from http://developer.android.com/guide/topics/data/data-storage.html
 *  Image view code based on http://developer.android.com/reference/android/widget/ImageView.html
 *  Alert dialog code based on example from http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/AlertDialogSamples.html
 *  
 */
package com.tagit;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class ViewRejectedPhoto extends Activity{

	private PhotosDbAdapter mPhotoDb;
	public static final String PREFS_NAME = "MyPrefsFile";
	Button btDone;
	Button btUnreject;
	SharedPreferences settings;
	int numAlgos;
	
	private long mPhotoId;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mPhotoDb = new PhotosDbAdapter(this);
		mPhotoDb.open();
		setContentView(R.layout.viewrejectedphoto);
		ImageView image = (ImageView) findViewById(R.id.imageView);
		Bundle extras = getIntent().getExtras();
		mPhotoId = extras.getLong(PhotosDbAdapter.KEY_ID);
		String path = extras.getString(PhotosDbAdapter.KEY_URI);
		Bitmap bm = BitmapFactory.decodeFile(path);
		image.setImageBitmap(bm);
		
		
		//From http://stackoverflow.com/questions/2795833/check-orientation-on-android-phone
		//Display getOrient = getWindowManager().getDefaultDisplay();
		Configuration config = getResources().getConfiguration();
		int orientation = config.orientation;
		
		//If orientation is portrait, can fill full screen
		if(orientation == Configuration.ORIENTATION_PORTRAIT){
			image.setScaleType(ImageView.ScaleType.FIT_XY);
		}else{
			image.setScaleType(ImageView.ScaleType.CENTER_INSIDE); //Otherwise, crop and center
		}

		
		numAlgos = 0; //Start with no  algorithmic Favorite Tags
		settings = getSharedPreferences(PREFS_NAME, 0);

		// Initialize reject and done buttons
		btUnreject = (Button) findViewById(R.id.unreject);
		btDone = (Button) findViewById(R.id.done);

		//Listener for done button
		btDone.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				Intent intent = new Intent(ViewRejectedPhoto.this, PhotoGallery.class);
				startActivity(intent);

			}
		});
		
		//Listener for unreject button
		btUnreject.setOnClickListener(new View.OnClickListener(){
			public void onClick(View v) {
				AlertDialog.Builder builder = createAlertDialog();
				AlertDialog alert = builder.create();
				alert.show();
				
			}
		});
		
	}
	
	private void CreateMenu(Menu menu)
	{
		menu.setQwertyMode(true);
		MenuItem mnu1 = menu.add(0, 0, 0, "Preferences");
		{
			mnu1.setAlphabeticShortcut('a');            
		}
		MenuItem mnu2 = menu.add(0, 1, 1, "Help");
		{
			mnu2.setAlphabeticShortcut('b');       
		}
	}
	
	private boolean MenuChoice(MenuItem item)
	{        
		switch (item.getItemId()) {
		case 0:
			goToPreferences();
			return true;
		case 1:
			goToHelp();
			return true;         
		}
		return false;
	} 

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		CreateMenu(menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{    
		return MenuChoice(item);    
	}

	public void goToPreferences(){
    	Intent intent = new Intent(ViewRejectedPhoto.this, Preferences.class);	
		startActivity(intent);	
    }

	public void goToHelp(){
		Intent intent = new Intent(ViewRejectedPhoto.this, Help.class);	
		startActivity(intent);
	}
	
	// Method to create a confirmation method to unreject a picture
	private AlertDialog.Builder createAlertDialog(){
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage("Are you sure you want to unreject the picture?")
		       .setCancelable(false)
		       .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int id) {
		        	    mPhotoDb.unrejectPhoto(mPhotoId);
						Intent intent = new Intent(ViewRejectedPhoto.this, PhotoGalleryRejected.class);
						startActivity(intent);
						
						//Toast message to let know the user he is on select mode
						Context context = getApplicationContext();
						CharSequence text = "You just unrejected a picture";
						int duration = Toast.LENGTH_LONG;
						Toast toast = Toast.makeText(context, text, duration);
						toast.show();
		           }
		       })
		       .setNegativeButton("No", new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int id) {
		                dialog.cancel();
		           }
		       });
		//AlertDialog alert = builder.create();
		return builder;
	}
	    
 

}
